package com.jiang.专题.动态规划.Q714;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/01/02 22:20
 */
class Solution {
    // 动态规划
    // public int maxProfit(int[] prices, int fee) {
    //     int len = prices.length;
    //     int[][] dp = new int[len][2]; // dp[i][0]手上没有股票最大利润，dp[i][1]表示持有股票最大利润
    //     dp[0][1] = -prices[0];
    //     for (int i = 1; i < len; i++) {
    //         dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i] - fee);
    //         dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
    //     }
    //     return dp[len - 1][0];
    // }

    // 贪心
    public int maxProfit(int[] prices, int fee) {
        int len = prices.length;
        int buy = prices[0] + fee;  // 在最大化收益的前提下,最低买入价格
        int maxProfit = 0;
        for (int i = 1; i < len; i++) {
            if (prices[i] + fee < buy) {
                buy = prices[i] + fee;
            } else if (prices[i] > buy) {
                maxProfit += prices[i] - buy;
                buy = prices[i];
            }
        }
        return maxProfit;
    }
}
